﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmCapNhatDmKhoa : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmKhoaEvent;
        int State = 0;
        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();
        DmKhoaBusiness dmKhoaBusiness = new DmKhoaBusiness();
        DmKhoaEntity dmKhoaEntity = null;

        public frmCapNhatDmKhoa(int state)
        {
            InitializeComponent();
            this.State = state;
        }

        public frmCapNhatDmKhoa(int state, DmKhoaEntity obj)
        {
            InitializeComponent();

            this.State = state;
            dmKhoaEntity = obj;

            txtMaKhoa.Text = dmKhoaEntity.MaKhoa;
            txtTenKhoa.Text = dmKhoaEntity.TenKhoa;
            txtDiaChi.Text = dmKhoaEntity.DiaChi;
            txtMoTa.Text = dmKhoaEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmKhoaEntity.TrangThaiSuDung;
            txtMaKhoa.Enabled = false;
            txtTenKhoa.Focus();
        }

        private void frmCapnhatDMKhoa_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaKhoa, "Mã khoa") == false) return;

            dmKhoaEntity = new DmKhoaEntity()
            {
                MaKhoa = txtMaKhoa.Text,
                TenKhoa = txtTenKhoa.Text.Trim(),
                DiaChi = txtDiaChi.Text.Trim(),
                MoTa = txtMoTa.Text.Trim(),
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked
            };
            if (this.State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmKhoaBusiness.GetDmKhoa(txtMaKhoa.Text) != null)
                {
                    if (Validation.ValidPrimaryKey(txtMaKhoa, "Mã khoa") == false) return;
                }
                dmKhoaBusiness.InsertDmKhoa(dmKhoaEntity);
                Validation.ClearControl(this);
                txtMaKhoa.Focus();
            }
            else
            {
                dmKhoaBusiness.EditDmKhoa(dmKhoaEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmKhoaEvent != null)
                dmKhoaEvent(sender, e);
        }


    }
}